(function(){"use strict";BX.namespace("BX.Landing.UI.Field");var t=BX.Landing.Utils.addClass;var e=BX.Landing.Utils.hasClass;var i=BX.Landing.Utils.removeClass;var n=BX.Landing.Utils.isArray;var a=BX.Landing.Utils.isFunction;var r=BX.Landing.Utils.create;var o=BX.Landing.Utils.random;var s=BX.Landing.Utils.escapeHtml;var l=BX.Landing.Utils.append;var u=BX.Landing.Utils.slice;var d=BX.Landing.Utils.encodeDataValue;var c=BX.Landing.Utils.remove;var p=BX.Landing.Utils.data;var h=BX.Landing.Utils.bind;var f=BX.Landing.Utils.style;var v=BX.Landing.Utils.clone;var g=BX.Landing.Utils.offsetLeft;var y=BX.Landing.Utils.offsetTop;var B=BX.Landing.Utils.findParent;var m=BX.Landing.Collection.BaseCollection;BX.Landing.UI.Field.Source=function(e){BX.Landing.UI.Field.BaseField.apply(this,arguments);t(this.layout,"landing-ui-field-source");this.fields=new m;this.onChangeHandler=a(e.onChange)?e.onChange:function(){};this.items=n(e.items)?e.items:[];this.value=e.value;if(!BX.type.isPlainObject(this.value)){this.value={source:this.items[0].value,filter:this.items[0].filter||[],sort:{}}}this.value.sort.items=this.getCurrentSource().sort.items;this.filterStub={key:"filterStub",name:BX.Landing.Loc.getMessage("LANDING_BLOCK__SOURCE_FILTER_STUB"),value:""};if(!BX.type.isArray(this.value.filter)||this.value.filter.length<=0){this.value.filter=[this.filterStub]}this.button=this.createButtonField();this.grid=this.createGrid(this.input,this.button.layout);this.sortByDropdown=this.createSortByField({items:this.value.sort.items,value:this.value.sort.value?this.value.sort.value.by:undefined});this.sortOrderDropdown=this.createSortOrderField(this.value.sort.value?this.value.sort.value.order:undefined);this.valueLayout=this.createValueLayout();this.valueLayoutWrapper=this.createValueLayoutWrapper(this.valueLayout);l(this.grid,this.layout);l(this.sortByDropdown.layout,this.layout);l(this.sortOrderDropdown.layout,this.layout);l(this.valueLayoutWrapper,this.header);this.setValue(this.value);this.currentSource=this.value.source;var i=BX.Landing.PageObject.getRootWindow();h(i.document,"click",this.onDocumentClick.bind(this));i.BX.addCustomEvent(i,"SidePanel.Slider:onMessage",this.onSliderMessageReducer.bind(this))};BX.Landing.UI.Field.Source.prototype={constructor:BX.Landing.UI.Field.Source,__proto__:BX.Landing.UI.Field.BaseField.prototype,getCurrentSource:function(){var t=this.value.source;return this.items.find(function(e){return e.value===t})},createGrid:function(t,e){return r("div",{props:{className:"landing-ui-field-source-grid"},children:[r("div",{props:{className:"landing-ui-field-source-grid-left"},children:[t]}),r("div",{props:{className:"landing-ui-field-source-grid-right"},children:[e]})]})},createValueLayout:function(){return r("span",{text:""})},createValueLayoutWrapper:function(t){return r("span",{children:[document.createTextNode(" ("),t,document.createTextNode(")")]})},createSortByField:function(t){return new BX.Landing.UI.Field.DropdownInline({title:BX.Landing.Loc.getMessage("LANDING_CARDS__SOURCE_FIELD_SORT_TITLE").toLowerCase(),items:t.items,content:t.value})},createSortOrderField:function(t){return new BX.Landing.UI.Field.DropdownInline({title:", ",items:[{name:BX.Landing.Loc.getMessage("LANDING_CARDS__SOURCE_FIELD_SORT_DESC"),value:"DESC"},{name:BX.Landing.Loc.getMessage("LANDING_CARDS__SOURCE_FIELD_SORT_ASC"),value:"ASC"}],content:t})},createButtonField:function(){return new BX.Landing.UI.Button.BaseButton("dropdown_button",{text:BX.Landing.Loc.getMessage("LINK_URL_SUGGESTS_SELECT"),className:"landing-ui-button-select-link",onClick:this.onButtonClick.bind(this)})},addPlaceholder:function(t){var e=r("div",{props:{className:"landing-ui-field-source-placeholder"},attrs:{"data-item":d(t),title:s(t.name)},children:[r("span",{props:{className:function(){if(!t.url){return"landing-ui-field-source-placeholder-text landing-ui-field-source-placeholder-text-plain"}return"landing-ui-field-source-placeholder-text"}()},html:s(t.name)}),function(){if(t.url){return r("span",{props:{className:"landing-ui-field-source-placeholder-remove"},events:{click:this.onPlaceholderRemoveClick.bind(this,t)}})}}.bind(this)()],events:{click:function(){if(t.url){this.onPlaceholderClick.bind(this,t.url)}return function(){}}.bind(this)()}});l(e,this.input)},openSourceFilterSlider:function(t){if(!t){this.onFilterSliderSave({getData:function(){return{filter:this.getValue().filter}}.bind(this)});return}var e=BX.Landing.Env.getInstance().getOptions().site_id;BX.SidePanel.Instance.open(t,{cacheable:false,requestMethod:"post",requestParams:{filter:this.getValue().filter,landingParams:{siteId:e}}})},onPlaceholderClick:function(t,e){e.stopPropagation();this.openSourceFilterSlider(t)},onSliderMessageReducer:function(t){switch(t.getEventId()){case"save":this.onFilterSliderSave(t);break;case"cancel":this.onFilterSliderCancel(t);break}},onFilterSliderSave:function(t){var e=t.getData();if(BX.type.isArray(e.filter)){var i=this.getValue();i.source=this.currentSource;i.filter=e.filter;this.setValue(i)}},onFilterSliderCancel:function(t){},getPlaceholders:function(){return[].slice.call(this.input.querySelectorAll(".landing-ui-field-source-placeholder"))},onPlaceholderRemoveClick:function(t,e,i){if(e){e.preventDefault();e.stopPropagation()}var n=this.getPlaceholderByItem(t);if(n){c(n)}if(!i){this.onValueChangeHandler(this)}var a=this.getPlaceholders();if(a.length<1){this.openSourceFilterSlider(t.url)}this.getPopup().close();this.popup=null},onMenuItemClick:function(t,e,i){i.getMenuWindow().close();var n=this.getItemByValue(this.items,t);if(Boolean(n)){this.currentSource=n.value;if(BX.type.isArray(n.filter)){var a=this.getValue();a.filter=BX.clone(n.filter);this.setValue(a,true)}BX.Dom.clean(this.input);this.openSourceFilterSlider(n.url)}this.popup=null},getPlaceholderByItem:function(t){return u(this.input.children).find(function(e){return JSON.stringify(p(e,"data-item").value)===JSON.stringify(t.value)})},getPopup:function(){if(this.popup){return this.popup}this.popup=new BX.Landing.UI.Tool.Menu({id:this.selector+"_"+o(),bindElement:this.button.layout,autoHide:true,items:this.items.map(function(t){return{id:t.value,text:BX.Landing.Utils.escapeText(t.name),onclick:this.onMenuItemClick.bind(this,t.value)}},this),events:{onPopupClose:function(){i(this.input,"landing-ui-active");if(e(this.layout.parentElement.parentElement,"landing-ui-form-style")){void f(this.layout.parentElement.parentElement,{"z-index":null,position:null})}}.bind(this)}});if(this.popup.popupWindow.popupContainer){t(this.popup.popupWindow.popupContainer,"landing-ui-field-source-popup");var n=B(this.input,{className:"landing-ui-panel-content-body-content"},document.body);if(n){l(this.popup.popupWindow.popupContainer,n)}}return this.popup},showPopup:function(){this.getPopup().show();this.adjustPopupPosition()},adjustPopupPosition:function(){if(this.popup){var t=B(this.button.layout,{className:"landing-ui-panel-content-body-content"});var e=y(this.button.layout,t);var i=g(this.button.layout,t);var n=this.button.layout.getBoundingClientRect();var a=this.popup.popupWindow.popupContainer.getBoundingClientRect();var r=2;var o=e+n.height+r;var s=i-(a.width-n.width);requestAnimationFrame(function(){this.popup.popupWindow.popupContainer.style.top=o+"px";this.popup.popupWindow.popupContainer.style.left=s+"px"}.bind(this))}},onInputClick:function(e){if(e){e.stopPropagation()}var n=this.getPopup();if(n.popupWindow.isShown()){i(this.input,"landing-ui-active");return n.close()}t(this.input,"landing-ui-active");return this.showPopup()},onButtonClick:function(t){t.preventDefault();t.stopPropagation();this.onInputClick()},onDocumentClick:function(){this.getPopup().close()},getValue:function(){var t=this.filterStub.key;var e=u(this.input.children).reduce(function(e,i){var n=p(i,"data-item");if(n.key!==t){e.push({key:n.key,name:n.name,value:n.value})}return e},[]);var i=this.valueLayout.dataset.value;var n=this.sortByDropdown.getValue();var a=this.sortOrderDropdown.getValue();return{source:i,filter:e,sort:{by:n,order:a}}},isChanged:function(){var t=v(this.value).sort();var e=this.getValue().sort();return JSON.stringify(t)!==JSON.stringify(e)},getItemByValue:function(t,e){var i=null;function n(t,e){return t.forEach(function(t){if(e==t.value){i=t;return}if(t.items){n(t.items,e)}},this)}n(t,e);return i},setValue:function(t,e){if(BX.type.isPlainObject(t)){if(BX.type.isArray(t.filter)){t.filter=t.filter.filter(function(t){return BX.type.isPlainObject(t)})}if(!BX.type.isArray(t.filter)||t.filter.length<=0){t.filter=[this.filterStub]}var i=this.getItemByValue(this.items,t.source);if(Boolean(i)){this.valueLayout.dataset.value=i.value;this.valueLayout.innerText=i.name;this.input.innerHTML="";var n=i.filter||[];if(BX.type.isArray(t.filter)){n=t.filter}n.forEach(function(t){if(BX.type.isPlainObject(t)){this.addPlaceholder(Object.assign({},t,{url:i.url}))}},this)}if(this.value.source!==i.value){this.value=i;BX.remove(this.sortByDropdown.layout);BX.remove(this.sortOrderDropdown.layout);this.sortByDropdown=this.createSortByField({items:this.value.sort.items,value:this.value.sort.value?this.value.sort.value.by:undefined});this.sortOrderDropdown=this.createSortOrderField(this.value.sort.value?this.value.sort.value.order:undefined);l(this.sortByDropdown.layout,this.layout);l(this.sortOrderDropdown.layout,this.layout)}}if(!e){this.onValueChangeHandler(this)}}}})();
//# sourceMappingURL=source_field.map.js